package com.kunbo.cn.service.impl.core.purchase;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import com.kunbo.cn.dao.core.purchase.SaleWaybillDao;
import com.kunbo.cn.entity.core.purchase.SaleWaybill;
import com.kunbo.cn.service.core.purchase.SaleWaybillService;


@Service
public class SaleWaybillServiceImpl implements SaleWaybillService {
	@Autowired
	private SaleWaybillDao waybillDao;
	@Autowired
	private JdbcTemplate jdbc;
	
	

	@Override
	public void save(SaleWaybill waybill) {
		waybillDao.save(waybill);

	}

	@Override
	public void update(SaleWaybill waybill) {
		waybillDao.save(waybill);

	}

	@Override
	public void delete(String id) {
		waybillDao.delete(id);

	}

	@Override
	public SaleWaybill findById(String id) {
		// TODO Auto-generated method stub
		return waybillDao.findOne(id);
	}

	@Override
	public Boolean waybillOrderCount(String code, float count, String state) {
		// TODO Auto-generated method stub
		String sql = "select capacity_to_be_transported from business_waybill_manage where waybill_code = '" +  code +"'";
		float a = 0;
		if(jdbc.queryForList(sql) != null  && jdbc.queryForList(sql).size() > 0 ) {
			if(jdbc.queryForList(sql).get(0).get("capacity_to_be_transported") != null) {
			a = Float.parseFloat(jdbc.queryForList(sql).get(0).get("capacity_to_be_transported").toString());
			}
			
		String updateSql = null;
		if(state.equals("true")) {
		if (a - count > 0 || a - count == 0) {
			updateSql = " update business_waybill_manage set capacity_to_be_transported = " + (a - count)
					+ " where waybill_code = '" +  code + "'";
		} else {
			updateSql = " update business_waybill_manage set capacity_to_be_transported = " + 0
					+ " where waybill_code = '" +  code + "'";
		}
		}else {
			updateSql = " update business_waybill_manage set capacity_to_be_transported = capacity_to_be_transported + " + count + " where waybill_code = '" +  code + "'";
		}
		
		int i = jdbc.update(updateSql);
		if (i > 0)
			return true;
		else
			return false;
	}
		return true;
	}

}